//
//  VisitorLoginView.swift
//  SynWeibo
//
//  Created by 沈艳男 on 15/9/1.
//  Copyright © 2015年 沈艳男. All rights reserved.
//

import UIKit

class VisitorLoginView: UIView {

    /**
    设置界面信息
    
    - parameter imageName: 图片信息
    - parameter message:   文字信息
    */
    func setUpInOf(imageName: String?, message: String){
        messageLabel.text = message
        
        if let imageName = imageName {
            // 转盘图片
            iconView.image = UIImage(named: imageName)
            // 如果不是主业酒吧，房子图片隐藏
            houseView.hidden = true
            // 删除蒙版
            maskIconView.removeFromSuperview()
            
        }else{
            // 转动转盘图标
            startAnimation()
        }
        
        
    }
    
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置旋转为360
        anim.toValue = 2 * M_PI
        // 设置每转一圈的时间
        anim.duration  = 1
        // 设置旋转的重复次数,MAXFLOAT就是无穷大
        anim.repeatCount = MAXFLOAT
        
        // 设置动画完成以后不被删除，已久可以运行，只有在iconView销毁的时候才会被释放
        anim.removedOnCompletion = false
        // 把需要旋转的控件放到主层上
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    
    // MARK: - 界面布局
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUi()
    }
    
    /// SB开发会被调用
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        
        setUpUi()
    }
    
    
    // 给控件设置自定布局
    private func setUpUi() {
        // 1、添加控件
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(houseView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 2、设置自定布局，将布局添加到视图上
        // "view1.attr1 = view2.attr2 * multiplier + constant"
        // 默认情况下，使用纯代码开发，是不支持自动布局的，如果要支持自动布局，需要将
        // 控件的 translatesAutoresizingMaskIntoConstraints 设置为 false / NO
        
        ///
        /// @param iconView                  给谁做自定布局
        /// @param NSLayoutAttribute.CenterX 设置被布局的属性
        /// @param NSLayoutRelation.Equal    ...
        /// @param self                      布局的参照物
        /// @param NSLayoutAttribute.CenterX 设置布局要参照的属性
        /// @param 1                         乘积，一般写1
        /// @param 0                         约束
        ///
        // 转盘图标
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -60))
        
        // 主页图标
        houseView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 文本信息设置
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        // 设置文本的宽度
        // 提示：如果要设置一个固定数值，参照的属性，需要设置为 NSLayoutAttribute.NotAnAttribute，参照对象是 nil
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:224))
        
        // 注册按钮的自定布局
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 44))
        
        // 登录按钮的自动布局
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 44))
        
        // 蒙版的布局
        // VFL 可视化布局语言
        /**
        H: 水平方向
        V: 垂直方向
        | 边界
        [] 控件
        
        metrics: 极少用
        views: [key, VFL 中[] 括起的名称, value: 控件] -> 控件和 VFL 的映射
        */
        maskIconView.translatesAutoresizingMaskIntoConstraints = false
        // 垂直方向距离左右两边的距离都是0
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[v]-0-|", options: [], metrics: nil, views: ["v":maskIconView]))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[v]-(-44)-[loginButton]", options: [], metrics: nil, views: ["v":maskIconView, "loginButton": loginButton]))
        
        // 自定义背景颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)

    }
    
    // 懒加载控件
    // 图标
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    // 小房子
    private lazy var houseView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    // 遮罩层
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    // 消息文字
    private lazy var messageLabel: UILabel = {
        // 实例化一个文字
        let label = UILabel()
        label.text = "一起尽在不言中"
        label.textColor = UIColor.darkGrayColor()
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        
        return label
    }()
    
    // 注册按钮
    lazy var registerButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)

        return button
    }()
    
    // 登录按钮
    lazy var loginButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        return button
        }()

}
